package com.wac.data.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wac.data.entity.Comment;
import com.wac.data.service.CommentService;
import com.wac.data.service.UserService;
import com.wac.data.utils.DataListUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author beta
 * @since 2023-03-16
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

	private static ObjectMapper objectMapper = new ObjectMapper();
	private Map<String, Object> commentList;

	@Resource
	private CommentService commentService;
	@Resource
	private UserService userService;

	/**
	 * 备注一下这个list 查实体id其实是查的列表 比如 动态id查的所有评论就是comment_id等于动态id的所有评论，文章同理
	 * 那实际上评论的评论他也是需要一个归属，数据库没有设计好这方面好巧不巧留了一个type字段，所以comment_id依然作为
	 * 归类id使用，用于查询这个实体有多少comment type则定位在comment的comment所回复的评论
	 */
	//1是升序 0是降序
	@RequestMapping("/list")
	public String getEntityCommentList(int page, String id, int order, HttpServletRequest request) throws JsonProcessingException {
		String userId = userService.isUserLogin(new DataListUtil().getCookies(request));
		return objectMapper.writeValueAsString(commentService.selectListById(page, id, order, userId));
	}

	@RequestMapping("/user")
	public String getUserCommentList(int page, String id) throws JsonProcessingException {
		return objectMapper.writeValueAsString(commentService.selectByUserId(page, id));
	}

	@RequestMapping("/add")
	public String addComment(@RequestBody Comment comment, HttpServletRequest request) {
		comment.setUserId(userService.isUserLogin(new DataListUtil().getCookies(request)));
		if (commentService.addComment(comment)) {
			return "1";
		}
		return "0";
	}

	@RequestMapping("/all_10")
	public String getCommentListTen() throws JsonProcessingException {
		return objectMapper.writeValueAsString(commentService.selectListAll());
	}

	@RequestMapping("/one")
	public String getComment(String id) throws JsonProcessingException {
		return objectMapper.writeValueAsString(commentService.selectById(id));
	}

	@RequestMapping("/delete")
	public String deleteComment(String id) {
		if (commentService.delete(id)) {
			return "1";
		}
		return "0";
	}
}

